<?
include("sys_config.php");
	define("HOST",$const_server);
	define("DB",$const_database);
	define("USER",$const_user);
	define("PWD",$const_password);
	date_default_timezone_set('Asia/Shanghai');//设置时区
	class DataAccess{
		public $link;
		var $res;//数据查询的结果集
		function getLink(){
			return $this->link;
		}
		//数据库连接方法
		function connect(){
			$this->link = mysql_connect(HOST,USER,PWD);
			if(!$this->link){
				die("数据库未初始化");
				//die('数据库连接失败！'.mysql_error());
			}
			mysql_select_db(DB,$this->link) or die ('不能够选择数据库'.mysql_error());
			return $this->link;
		}
		//释放数据库连接
		function close(){
			//释放结果集
			if($this->res)
			mysql_free_result($this->res);
			//关闭连接
			if($this->link)
			mysql_close($this->link);
		}
		//获取记录集函数
		function getRowNum(){
			return mysql_num_rows($this->res);
		}
	    //数据查询函数
		function &query($sql){
			mysql_query("set names 'utf8'");
			$this->res = mysql_query($sql) or die ('数据库查询错误'.mysql_error());
			return $this->res;
		}
		//数据增、删、改函数
		function &perform($sql){
			mysql_query("set names 'utf8'");
			$this->res = mysql_query($sql) or die ('数据库查询错误'.mysql_error());
			if($this->res)
				return true;
			return false;
		}
	}


	/**
   * 转义数据
   * @param $value 要转义的数据
   */
  function addslashes_deep($value) {
      if (is_array($value)) {
          $return = array_map("addslashes_deep", $value);
          return $return;
      } else {
          $return = addslashes($value);
          return $return;
      }
  }

  /**
   * 反转义数据
   * @param $value 要反转义的数据
   */
  function stripslashes_deep($value) {
      if (is_array($value)) {
          $return = array_map('stripslashes_deep',$value);
          return $return;
      } else {
          $return = stripslashes($value);
          return $return ;
      }
  }
//字符串乱码处理
function getstr($string, $length, $encoding  = 'utf-8') {
    $string = trim($string);

    if($length && strlen($string) > $length) {
        //截断字符
        $wordscut = '';
        if(strtolower($encoding) == 'utf-8') {
            //utf8编码
            $n = 0;
            $tn = 0;
            $noc = 0;
            while ($n < strlen($string)) {
                $t = ord($string[$n]);
                if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
                    $tn = 1;
                    $n++;
                    $noc++;
                } elseif(194 <= $t && $t <= 223) {
                    $tn = 2;
                    $n += 2;
                    $noc += 2;
                } elseif(224 <= $t && $t < 239) {
                    $tn = 3;
                    $n += 3;
                    $noc += 2;
                } elseif(240 <= $t && $t <= 247) {
                    $tn = 4;
                    $n += 4;
                    $noc += 2;
                } elseif(248 <= $t && $t <= 251) {
                    $tn = 5;
                    $n += 5;
                    $noc += 2;
                } elseif($t == 252 || $t == 253) {
                    $tn = 6;
                    $n += 6;
                    $noc += 2;
                } else {
                    $n++;
                }
                if ($noc >= $length) {
                    break;
                }
            }
            if ($noc > $length) {
                $n -= $tn;
            }
            $wordscut = substr($string, 0, $n);
        } else {
            for($i = 0; $i < $length - 1; $i++) {
                if(ord($string[$i]) > 127) {
                    $wordscut .= $string[$i].$string[$i + 1];
                    $i++;
                } else {
                    $wordscut .= $string[$i];
                }
            }
        }
        $string = $wordscut;
    }
    return trim($string);
}
//字符串乱码处理
function GBsubstr($string, $start, $length) {
if(strlen($string)>$length){
   $str=null;
   $len=$start+$length;
   for($i=$start;$i<$len;$i++){
    if(ord(substr($string,$i,1))>0xa0){
     $str.=substr($string,$i,2);
     $i++;
    }else{
     $str.=substr($string,$i,1);
    }
   }
   //return $str.'...';
   return $str;
}else{
   return $string;
}
}
